package com.jilian.mydemo.modul.order.ui;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jilian.mydemo.MyApplication;
import com.jilian.mydemo.R;
import com.jilian.mydemo.base.BaseActivity;
import com.jilian.mydemo.base.BaseDto;
import com.jilian.mydemo.common.dto.FinishPayDto;
import com.jilian.mydemo.common.dto.OrderDetailDto;
import com.jilian.mydemo.common.dto.OrderDto;
import com.jilian.mydemo.modul.order.viewmodel.OrderViewModel;
import com.jilian.mydemo.utils.DateUtil;
import com.jilian.mydemo.utils.MainRxTimerUtil;
import com.jilian.mydemo.utils.ToastUitl;

import java.util.Date;

public class PayOrderActivity extends BaseActivity {
    private RelativeLayout rlAlipay;
    private RelativeLayout rlWechat;
    private RelativeLayout rlCash;
    private TextView tvHour;
    private TextView tvMin;
    private TextView tvSecond;
    private TextView tvMoney;
    private OrderViewModel viewModel;


    private String orderId;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.removeActivity(this);
        MainRxTimerUtil.cancel();
    }

    @Override
    protected void createViewModel() {
        viewModel = ViewModelProviders.of(this).get(OrderViewModel.class);
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_payorder;
    }

    private long fifityMin = 60 * 60 * 1000l;//一个小时

    @Override
    public void initView() {
        setNormalTitle("支付订单", v -> finish());
        orderId = getIntent().getStringExtra("orderId");

        rlAlipay = (RelativeLayout) findViewById(R.id.rl_alipay);
        rlWechat = (RelativeLayout) findViewById(R.id.rl_wechat);
        rlCash = (RelativeLayout) findViewById(R.id.rl_cash);
        tvHour = (TextView) findViewById(R.id.tv_hour);
        tvMin = (TextView) findViewById(R.id.tv_min);
        tvSecond = (TextView) findViewById(R.id.tv_second);
        tvMoney = (TextView) findViewById(R.id.tv_money);
        tvMoney.setText(getIntent().getStringExtra("totalMoney"));
    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {
        rlAlipay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finishPay("1");
                //支付宝

            }
        });
        rlWechat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //微信
                finishPay("2");

            }
        });
        rlCash.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //现金
                Intent intent = new Intent(PayOrderActivity.this, PayUploadActivity.class);
                intent.putExtra("orderDto", getIntent().getSerializableExtra("orderDto"));
                intent.putExtra("orderId", orderId);
                intent.putExtra("delTime", delTime);
                intent.putExtra("totalMoney", getIntent().getStringExtra("totalMoney"));
                intent.putExtra("type", "3");
                startActivity(intent);
                finish();


            }
        });
    }

    private void finishPay(String type) {
        showLoadingDialog();
        viewModel.finishPay(orderId, type, null);
        viewModel.getFinishPayliveData().observe(this, new Observer<BaseDto<FinishPayDto>>() {
            @Override
            public void onChanged(@Nullable BaseDto<FinishPayDto> baseDto) {
                hideLoadingDialog();
                if (baseDto.isSuccess()) {
                    String url = baseDto.getData().getCodeUrl();
                    //微信
                    Intent intent = new Intent(PayOrderActivity.this, PayQcodeActivity.class);
                    intent.putExtra("orderDto", getIntent().getSerializableExtra("orderDto"));
                    intent.putExtra("orderId", orderId);
                    intent.putExtra("order", orderId);
                    intent.putExtra("url", url);
                    intent.putExtra("type", type);
                    intent.putExtra("delTime", delTime);
                    intent.putExtra("totalMoney", getIntent().getStringExtra("totalMoney"));
                    startActivity(intent);
                    finish();
                } else {
                    ToastUitl.showImageToastTips(baseDto.getMsg());
                }

            }
        });
    }

    /**
     * 开启倒计时
     *
     * @param endTime
     */
    long delTime = 0;

    private void startTimeTask(long endTime) {
        MainRxTimerUtil.interval(1000, new MainRxTimerUtil.IRxNext() {
            @Override
            public void doNext() {//获取现在的 时分秒 时间戳
                long nowTime = new Date().getTime();
                //单位 s
                delTime = endTime - nowTime;
                if (delTime > 0) {
                    String str = DateUtil.timeToHms(delTime);
                    tvHour.setText(str.split(":")[0]);
                    tvMin.setText(str.split(":")[1]);
                    tvSecond.setText(str.split(":")[2]);

                } else {
                    finish();
                }

            }
        });
    }
}
